package Control;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;

import Interfaces.Zoom;

/**
 * 
 * @author stephenbwilliamson
 */
public class Zoomable implements Zoom {

	private static Zoomable instance;
	private double numPositions;
	private double autoZoom;

	private List<JComponent> updateOnChange;

	private int windowWidth;

	protected Zoomable() {
		numPositions = -1;
		updateOnChange = new ArrayList<JComponent>();
	}

	public static Zoomable getInstance() {
		if (instance == null) {
			instance = new Zoomable();
		}
		return instance;
	}

	public void registerComponent(JComponent component) {
		updateOnChange.add(component);
	}

	public boolean isSet() {
		return numPositions != -1;
	}

	public void resetZoom() {
		numPositions = -1;
		repaintComponents();
	}

	private void repaintComponents() {
		for (JComponent comp : updateOnChange) {
			comp.repaint();
		}
	}

	public List<JComponent> getUpdateList() {
		return updateOnChange;
	}

	@Override
	public void zoomOut() {
		numPositions++;
		if (numPositions > autoZoom)
			numPositions = autoZoom;
		repaintComponents();
	}

	@Override
	public void zoomIn() {
		numPositions--;
		if (numPositions < 2)
			numPositions = 2;
		repaintComponents();
	}

	public void setWidth(int window) {
		windowWidth = window - 40;
	}

	public void autoZoom(int geneLength) {
		numPositions = geneLength * 1.0 / windowWidth;
		autoZoom = numPositions;
	}

	public double getPositions() {
		return Math.round(numPositions);
	}

	public int calcDist(int baseDistance) {
		return (int) Math.round(baseDistance * 1.0 / numPositions);
	}
}
